package cn.mymall.web.controller;

import cn.mymall.constant.OrderStatusConstant;
import cn.mymall.entity.Member;
import cn.mymall.entity.MemberAddress;
import cn.mymall.entity.TradeOrder;
import cn.mymall.entity.mall.CartItem;
import cn.mymall.entity.mall.Product;
import cn.mymall.entity.mall.Sku;
import cn.mymall.service.IMemberAddressService;
import cn.mymall.service.IMemberService;
import cn.mymall.service.ITradeOrderService;
import cn.mymall.service.OrderException;
import cn.mymall.service.mall.ICartItemService;
import cn.mymall.service.mall.IProductService;
import cn.mymall.service.mall.ISkuService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.vo.order.CreateCartOrderVo;
import cn.mymall.vo.order.CreateMallOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class OrderController extends BaseController{
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberAddressService memberAddressService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ITradeOrderService orderService;
    @Autowired
    private ICartItemService cartItemService;
    @Autowired
    private ISkuService skuService;

    @RequestMapping("/home/order/create")
    @ResponseBody
    public ResultCode create(CreateMallOrderVo orderVo) {
        if (orderVo.getQty() == null) {
            return ResultCode.getFailure("缺少参数qty");
        }
        if (orderVo.getSkuId() == null) {
            return ResultCode.getFailure("缺少参数skuId");
        }
        if (orderVo.getAddressId() == null) {
            return ResultCode.getFailure("缺少参数addressId");
        }
        Member member = getLoginMember();
        Sku sku = skuService.getById(orderVo.getSkuId());
        Product product = productService.getById(sku.getProductId());
        MemberAddress memberAddress = memberAddressService.getById(orderVo.getAddressId());
        if (memberAddress == null) {
            return ResultCode.getFailure("收货地址不存在");
        }
        if(!memberAddress.getMemberId().equals(member.getId())){
            return ResultCode.getFailure("非法操作");
        }
        orderVo.setMemberId(member.getId());
        orderVo.setProductId(sku.getProductId());
        orderVo.setMobile(member.getMobile());
        orderVo.setMemberAddress(memberAddress);
        orderVo.setIp(BaseWebUtil.getRemoteIp(getRequest()));
        try {
            TradeOrder order = orderService.createOrder(orderVo);
            return ResultCode.getSuccessReturn(order.getId());
        } catch (OrderException e) {
            e.printStackTrace();
            return ResultCode.getFailure(e.getMsg());
        }
    }

    @RequestMapping("/home/order/create/byCart")
    @ResponseBody
    public ResultCode createByCart(CreateCartOrderVo orderVo) {
        if (orderVo.getAddressId() == null) {
            return ResultCode.getFailure("缺少参数addressId");
        }
        Member member = getLoginMember();
        if(member==null){
            return ResultCode.getFailure("用户不存在");
        }
        MemberAddress memberAddress = memberAddressService.getById(orderVo.getAddressId());
        if (memberAddress == null) {
            return ResultCode.getFailure("收货地址不存在");
        }
        if(!memberAddress.getMemberId().equals(member.getId())){
            return ResultCode.getFailure("非法操作");
        }
        List<CartItem> cartItemList = cartItemService.listByIds(Arrays.asList(orderVo.getCartIds().split(",")));
        CreateMallOrderVo createMallOrderVo = new CreateMallOrderVo();
        createMallOrderVo.setMemberId(member.getId());
        createMallOrderVo.setMobile(member.getMobile());
        createMallOrderVo.setMemberAddress(memberAddress);
        createMallOrderVo.setIp(BaseWebUtil.getRemoteIp(getRequest()));
        try {
            TradeOrder order = orderService.createOrderByCart(createMallOrderVo, new ArrayList<>(cartItemList));
            return ResultCode.getSuccessReturn(order.getId());
        } catch (OrderException e) {
            e.printStackTrace();
            return ResultCode.getFailure(e.getMsg());
        }
    }

    @RequestMapping("/home/order/pay")
    public String pay(Integer orderId, ModelMap model){
        TradeOrder order = orderService.getById(orderId);
        model.put("order", order);
        return "front/home/pay.ftl";
    }

    @RequestMapping("/home/order/payResult")
    @ResponseBody
    public ResultCode payResult(Integer orderId, String paymethod){
        Member member = getLoginMember();
        if(member==null){
            return ResultCode.getFailure("用户不存在");
        }
        TradeOrder order = orderService.getById(orderId);
        order.setPayStatus(OrderStatusConstant.PAID);
        order.setOrderStatus(OrderStatusConstant.WAIT_SNED);
        order.setPaymethod(paymethod);
        orderService.updateById(order);
        return ResultCode.SUCCESS;
    }

    @RequestMapping("/home/paySuccess")
    public String paySuccess() {
        return "front/home/pay_success.ftl";
    }
}
